<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
  <title>Description of fvcom2swan</title>
  <meta name="keywords" content="fvcom2swan">
  <meta name="description" content="Convert fvcom grid and bathymetry file to unstructured SWAN format">
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta name="generator" content="m2html v1.5 &copy; 2003-2005 Guillaume Flandin">
  <meta name="robots" content="index, follow">
  <link type="text/css" rel="stylesheet" href="../m2html.css">
</head>
<body>
<a name="_top"></a>
<div><a href="../index.html">Home</a> &gt;  <a href="index.html">swan_scripts</a> &gt; fvcom2swan.m</div>

<!--<table width="100%"><tr><td align="left"><a href="../index.html"><img alt="<" border="0" src="../left.png">&nbsp;Master index</a></td>
<td align="right"><a href="index.html">Index for swan_scripts&nbsp;<img alt=">" border="0" src="../right.png"></a></td></tr></table>-->

<h1>fvcom2swan
</h1>

<h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="box"><strong>Convert fvcom grid and bathymetry file to unstructured SWAN format</strong></div>

<h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="box"><strong>function fvcom2swan(fvcom_mesh,fvcom_bathy,fvcom_obc,prefix,PlotMesh) </strong></div>

<h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="fragment"><pre class="comment"> Convert fvcom grid and bathymetry file to unstructured SWAN format 

 function fvcom2swan(fvcom_mesh,fvcom_bathy,fvcom_obc,prefix)

 DESCRIPTION:
    convert FVCOM mesh and bathymetry to SWAN format mesh and bathymetry

 INPUT 
   fvcom_mesh  = FVCOM 3.x grid file 
   fvcom_bathy = FVCOM 3.x bathymetry file
   fvcom_obc   = FVCOM 3.x open boundary file
   prefix      = prefix for naming SWAN grid and depth files   
   PlotMesh    = [true,false] plot the resulting mesh          

 OUTPUT:
    prefix.bot  = swan bathymetry file
    prefix.node = swan vertex file
    prefix.ele  = swan connectivity file

 EXAMPLE USAGE
    fvcom2swan('tst_grd.dat','tst_dep.dat','tst_obc.dat','tst',true) 

 Author(s):  
    Geoff Cowles (University of Massachusetts Dartmouth)

 Revision history
   
==============================================================================</pre></div>

<!-- crossreference -->
<h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
This function calls:
<ul style="list-style-image:url(../matlabicon.gif)">
<li><a href="plot_swan_mesh.html" class="code" title="function plot_swan_mesh(bathfile,nodefile,gridfile)">plot_swan_mesh</a>	Plot an unstructured SWAN mesh</li></ul>
This function is called by:
<ul style="list-style-image:url(../matlabicon.gif)">
</ul>
<!-- crossreference -->



<h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="fragment"><pre>0001 <a name="_sub0" href="#_subfunctions" class="code">function fvcom2swan(fvcom_mesh,fvcom_bathy,fvcom_obc,prefix,PlotMesh)</a>
0002 <span class="comment">% Convert fvcom grid and bathymetry file to unstructured SWAN format</span>
0003 <span class="comment">%</span>
0004 <span class="comment">% function fvcom2swan(fvcom_mesh,fvcom_bathy,fvcom_obc,prefix)</span>
0005 <span class="comment">%</span>
0006 <span class="comment">% DESCRIPTION:</span>
0007 <span class="comment">%    convert FVCOM mesh and bathymetry to SWAN format mesh and bathymetry</span>
0008 <span class="comment">%</span>
0009 <span class="comment">% INPUT</span>
0010 <span class="comment">%   fvcom_mesh  = FVCOM 3.x grid file</span>
0011 <span class="comment">%   fvcom_bathy = FVCOM 3.x bathymetry file</span>
0012 <span class="comment">%   fvcom_obc   = FVCOM 3.x open boundary file</span>
0013 <span class="comment">%   prefix      = prefix for naming SWAN grid and depth files</span>
0014 <span class="comment">%   PlotMesh    = [true,false] plot the resulting mesh</span>
0015 <span class="comment">%</span>
0016 <span class="comment">% OUTPUT:</span>
0017 <span class="comment">%    prefix.bot  = swan bathymetry file</span>
0018 <span class="comment">%    prefix.node = swan vertex file</span>
0019 <span class="comment">%    prefix.ele  = swan connectivity file</span>
0020 <span class="comment">%</span>
0021 <span class="comment">% EXAMPLE USAGE</span>
0022 <span class="comment">%    fvcom2swan('tst_grd.dat','tst_dep.dat','tst_obc.dat','tst',true)</span>
0023 <span class="comment">%</span>
0024 <span class="comment">% Author(s):</span>
0025 <span class="comment">%    Geoff Cowles (University of Massachusetts Dartmouth)</span>
0026 <span class="comment">%</span>
0027 <span class="comment">% Revision history</span>
0028 <span class="comment">%</span>
0029 <span class="comment">%==============================================================================</span>
0030 
0031 subname = <span class="string">'fvcom2swan'</span>;
0032 fprintf(<span class="string">'\n'</span>)
0033 fprintf([<span class="string">'begin : '</span> subname <span class="string">'\n'</span>])
0034 
0035 <span class="comment">% setup SWAN output files</span>
0036 swan_bathy  = [prefix,<span class="string">'.bot'</span>];
0037 swan_node   = [prefix,<span class="string">'.node'</span>];
0038 swan_ele    = [prefix,<span class="string">'.ele'</span>];
0039 
0040 <span class="comment">%------------------------------------------------------------------------------</span>
0041 <span class="comment">% read in the FVCOM bathymetry data</span>
0042 <span class="comment">%------------------------------------------------------------------------------</span>
0043 fid = fopen(fvcom_bathy,<span class="string">'r'</span>);
0044 <span class="keyword">if</span>(fid  &lt; 0)
0045   error([<span class="string">'file: '</span> fvcom_bathy <span class="string">' does not exist'</span>]);
0046 <span class="keyword">end</span>;
0047 C = textscan(fid, <span class="string">'%s %s %s %d'</span>, 1);
0048 Nverts = C{4};
0049 h = zeros(Nverts,1);
0050 fprintf(<span class="string">'reading bathymetry file\n'</span>);
0051 fprintf(<span class="string">'# nodes %d\n'</span>,Nverts);
0052 <span class="keyword">for</span> i=1:Nverts
0053   C = textscan(fid, <span class="string">'%f %f %f'</span>, 1);
0054   h(i) = C{3};
0055 <span class="keyword">end</span>;
0056 fprintf(<span class="string">'min depth %f max depth %f\n'</span>,min(h),max(h));
0057 fprintf(<span class="string">'bathymetry reading complete\n'</span>);
0058 fclose(fid);
0059 
0060 <span class="comment">%------------------------------------------------------------------------------</span>
0061 <span class="comment">% read in the FVCOM open boundary node data</span>
0062 <span class="comment">%------------------------------------------------------------------------------</span>
0063 fid = fopen(fvcom_obc,<span class="string">'r'</span>);
0064 <span class="keyword">if</span>(fid  &lt; 0)
0065   error([<span class="string">'file: '</span> fvcom_obc <span class="string">' does not exist'</span>]);
0066 <span class="keyword">end</span>;
0067 C = textscan(fid, <span class="string">'%s %s %s %s %d'</span>, 1);
0068 Nobcs = C{5};
0069 obc_nodes = zeros(Nobcs,1);
0070 fprintf(<span class="string">'reading obc file\n'</span>);
0071 fprintf(<span class="string">'# nodes %d\n'</span>,Nobcs);
0072 <span class="keyword">for</span> i=1:Nobcs
0073   C = textscan(fid, <span class="string">'%d %d %d'</span>, 1);
0074   obc_nodes(i) = C{2};
0075 <span class="keyword">end</span>;
0076 
0077 fprintf(<span class="string">'obc reading complete\n'</span>);
0078 fclose(fid);
0079 
0080 <span class="comment">%----------------------------------------------------</span>
0081 <span class="comment">% read in the fvcom connectivity and vertices</span>
0082 <span class="comment">%----------------------------------------------------</span>
0083 fid = fopen(fvcom_mesh,<span class="string">'r'</span>);
0084 <span class="keyword">if</span>(fid  &lt; 0)
0085   error([<span class="string">'file: '</span> fvcom_mesh <span class="string">' does not exist'</span>]);
0086 <span class="keyword">end</span>;
0087 C = textscan(fid, <span class="string">'%s %s %s %d'</span>, 1); Nverts = C{4};
0088 C = textscan(fid, <span class="string">'%s %s %s %d'</span>, 1); Nelems = C{4};
0089 tri = zeros(Nelems,3); 
0090 x   = zeros(Nverts,2);
0091 fprintf(<span class="string">'reading mesh file\n'</span>);
0092 fprintf(<span class="string">'# nodes %d\n'</span>,Nverts);
0093 fprintf(<span class="string">'# elems %d\n'</span>,Nelems);
0094 <span class="keyword">for</span> i=1:Nelems
0095   C = textscan(fid,<span class="string">' %d %d %d %d %d\n'</span>,1);
0096   tri(i,1) = C{2};  tri(i,2) = C{3}; tri(i,3) = C{4};
0097 <span class="keyword">end</span>;
0098 <span class="keyword">for</span> i=1:Nverts 
0099   C = textscan(fid, <span class="string">'%d %f %f %f'</span>, 1);
0100   x(i,1) = C{2};
0101   x(i,2) = C{3};
0102 <span class="keyword">end</span>;
0103 fprintf(<span class="string">'mesh read in\n'</span>);
0104 fclose(fid);
0105 
0106 <span class="comment">%----------------------------------------------------</span>
0107 <span class="comment">% mark nodes on the boundary</span>
0108 <span class="comment">%----------------------------------------------------</span>
0109 bnodes = zeros(Nverts,1);
0110 cells = zeros(Nverts,10);
0111 cellcnt = zeros(Nverts,1);
0112 nbe = zeros(Nelems,3);
0113 
0114 <span class="keyword">for</span> i = 1:Nelems
0115     n1 = tri(i,1) ; cellcnt(n1) = cellcnt(n1) + 1;
0116     n2 = tri(i,2) ; cellcnt(n2) = cellcnt(n2) + 1;
0117     n3 = tri(i,3) ; cellcnt(n3) = cellcnt(n3) + 1;
0118     cells(tri(i,1),cellcnt(n1)) = i;
0119     cells(tri(i,2),cellcnt(n2)) = i;
0120     cells(tri(i,3),cellcnt(n3)) = i;
0121 <span class="keyword">end</span>;
0122 
0123 <span class="keyword">if</span>(max(cellcnt) &gt; 10)
0124   error(<span class="string">'increase cells array'</span>)
0125 <span class="keyword">end</span>;
0126 
0127 <span class="keyword">for</span> i = 1:Nelems
0128     n1 = tri(i,1); n2 = tri(i,2); n3 = tri(i,3);
0129     <span class="keyword">for</span> j1 = 1:cellcnt(n1)
0130         <span class="keyword">for</span> j2 = 1:cellcnt(n2)
0131             <span class="keyword">if</span>((cells(n1,j1) == cells(n2,j2)) &amp; cells(n1,j1) ~= i); nbe(i,3) = cells(n1,j1); <span class="keyword">end</span>;
0132         <span class="keyword">end</span>;
0133     <span class="keyword">end</span>;
0134     <span class="keyword">for</span> j2 = 1:cellcnt(n2)
0135         <span class="keyword">for</span> j3 = 1:cellcnt(n3)
0136             <span class="keyword">if</span>((cells(n2,j2) == cells(n3,j3)) &amp; cells(n2,j2) ~= i); nbe(i,1) = cells(n2,j2); <span class="keyword">end</span>;
0137         <span class="keyword">end</span>;
0138     <span class="keyword">end</span>;
0139     <span class="keyword">for</span> j1 = 1:cellcnt(n1)
0140         <span class="keyword">for</span> j3 = 1:cellcnt(n3)
0141              <span class="keyword">if</span>((cells(n1,j1) == cells(n3,j3)) &amp; cells(n1,j1) ~= i); nbe(i,2) = cells(n3,j3); <span class="keyword">end</span>;
0142         <span class="keyword">end</span>;
0143     <span class="keyword">end</span>;
0144 <span class="keyword">end</span>;
0145 
0146 
0147 check = Nelems/10;
0148 <span class="keyword">for</span> i=1:Nelems
0149    n1 = tri(i,1); n2 = tri(i,2); n3 = tri(i,3);
0150    <span class="keyword">if</span>(nbe(i,1) == 0)
0151      bnodes(n2) = 1; bnodes(n3) = 1;
0152    <span class="keyword">elseif</span>(nbe(i,2) == 0)
0153      bnodes(n3) = 1; bnodes(n1) = 1; 
0154    <span class="keyword">elseif</span>(nbe(i,3) == 0)
0155      bnodes(n1) = 1; bnodes(n2) = 1; 
0156    <span class="keyword">end</span>;
0157    <span class="keyword">if</span>(mod(i,check)==0); fprintf(<span class="string">'bnodes: completed %f percent \n'</span>,100*i/Nelems); <span class="keyword">end</span>;
0158 <span class="keyword">end</span>;
0159 
0160 <span class="comment">%----------------------------------------------------</span>
0161 <span class="comment">% Mark open boundary with value of 2</span>
0162 <span class="comment">%----------------------------------------------------</span>
0163 <span class="keyword">if</span>(Nobcs &gt; 1)
0164    bnodes(obc_nodes) = 2;
0165 <span class="keyword">end</span>;
0166 <span class="comment">%----------------------------------------------------</span>
0167 <span class="comment">% dump swan bathymetry file</span>
0168 <span class="comment">%----------------------------------------------------</span>
0169 fid = fopen(swan_bathy,<span class="string">'w'</span>);
0170 <span class="keyword">for</span> i=1:Nverts
0171     fprintf(fid,<span class="string">'%f\n'</span>,h(i));
0172 <span class="keyword">end</span>;
0173 fclose(fid);
0174 
0175 
0176 <span class="comment">%----------------------------------------------------</span>
0177 <span class="comment">% dump swan node file</span>
0178 <span class="comment">%----------------------------------------------------</span>
0179 fid = fopen(swan_node,<span class="string">'w'</span>);
0180 fprintf(fid,<span class="string">'%d 2 0 1\n'</span>,Nverts);
0181 <span class="keyword">for</span> i=1:Nverts
0182     fprintf(fid,<span class="string">'%d %f %f %d\n'</span>,i,x(i,1),x(i,2),bnodes(i)); 
0183 <span class="keyword">end</span>;
0184 fclose(fid);
0185 
0186 
0187 <span class="comment">%----------------------------------------------------</span>
0188 <span class="comment">% dump swan connectivty file</span>
0189 <span class="comment">%----------------------------------------------------</span>
0190 fid = fopen(swan_ele,<span class="string">'w'</span>);
0191 fprintf(fid,<span class="string">'%d 3 0\n'</span>,Nelems);
0192 <span class="keyword">for</span> i=1:Nelems 
0193     fprintf(fid,<span class="string">'%d %d %d %d\n'</span>,i,tri(i,1:3)); 
0194 <span class="keyword">end</span>;
0195 fclose(fid);
0196 
0197 <span class="comment">%----------------------------------------------------</span>
0198 <span class="comment">% plot mesh from swan files to check</span>
0199 <span class="comment">%----------------------------------------------------</span>
0200 <span class="keyword">if</span>(PlotMesh)
0201     <a href="plot_swan_mesh.html" class="code" title="function plot_swan_mesh(bathfile,nodefile,gridfile)">plot_swan_mesh</a>(swan_bathy,swan_node,swan_ele)
0202 <span class="keyword">end</span>;
0203 
0204 fprintf([<span class="string">'end   : '</span> subname <span class="string">'\n'</span>])
0205</pre></div>
<hr><address>Generated on Wed 02-Nov-2011 21:58:00 by <strong><a href="http://www.artefact.tk/software/matlab/m2html/" title="Matlab Documentation in HTML">m2html</a></strong> &copy; 2005</address>
</body>
</html>